package com.umeng.analytics.pro;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

class c extends SQLiteOpenHelper {
    static class c$a {
        private static final c a = new c(c.a(), d.a(c.a()), "cc.db", null, 1);

        private c$a() {

        }

        static c a() {
            return a;
        }
    }

    private String a;
    private static Context b;

    private c(Context arg2, String arg3, SQLiteDatabase.CursorFactory arg4, int arg5) {
        super(arg2, arg3 == null || (arg3.equals("")) ? "cc.db" : arg3, arg4, arg5);
        this.b();
    }

    private c(Context arg2, String arg3, String arg4, SQLiteDatabase.CursorFactory arg5, int arg6) {
        this(new e(arg2, arg3), arg4, arg5, arg6);
    }

    public static c a(Context arg2) {
        c v0_1;
        Class v1 = c.class;
        synchronized (v1) {
            c.b = arg2;
            v0_1 = c$a.a();
        }
        return v0_1;
    }

    static Context a() {
        return c.b;
    }

    private boolean a(SQLiteDatabase arg2) {
        boolean v0_1;
        try {
            this.a = "create table if not exists limitedck(Id INTEGER primary key autoincrement, ck TEXT unique)";
            arg2.execSQL(this.a);
            v0_1 = true;
        } catch (SQLException v0) {
            by.e("create reference table error!");
            v0_1 = false;
        }

        return v0_1;
    }

    public boolean a(String arg5, SQLiteDatabase arg6) {
        Cursor v1 = null;
        boolean v0 = false;
        if (arg5 != null) {
            try {
                v1 = arg6.rawQuery("select count(*) as c from sqlite_master where type =\'table\' and name =\'" + arg5.trim() + "\' ", null);
                if (v1.moveToNext()) {
                    if (v1.getInt(0) <= 0) {
                        return false;
                    }
                    return true;
                }
                return false;
            } catch (Exception v2) {
                return false;
            } finally {
                if (v1 != null) {
                    try {
                        v1.close();
                    } catch (Exception e) {

                    }
                }
            }
        }

        return v0;
    }

    private void b() {
        try {
            SQLiteDatabase v0_2 = this.getWritableDatabase();
            if (!this.a("aggregated", v0_2) || !this.a("aggregated_cache", v0_2)) {
                this.c(v0_2);
            }

            if (!this.a("system", v0_2)) {
                this.b(v0_2);
            }

            if (this.a("limitedck", v0_2)) {
                return;
            }
            this.a(v0_2);
        } catch (Exception v0_1) {

        }
    }

    private boolean b(SQLiteDatabase arg2) {
        boolean v0_1;
        try {
            this.a = "create table if not exists system(Id INTEGER primary key autoincrement, key TEXT, timeStamp INTEGER, count INTEGER)";
            arg2.execSQL(this.a);
            v0_1 = true;
        } catch (SQLException v0) {
            by.e("create system table error!");
            v0_1 = false;
        }

        return v0_1;
    }

    private boolean c(SQLiteDatabase arg2) {
        boolean v0_1;
        try {
            this.a = "create table if not exists aggregated_cache(Id INTEGER primary key autoincrement, key TEXT, totalTimestamp TEXT, value INTEGER, count INTEGER, label TEXT, timeWindowNum TEXT)";
            arg2.execSQL(this.a);
            this.a = "create table if not exists aggregated(Id INTEGER primary key autoincrement, key TEXT, totalTimestamp TEXT, value INTEGER, count INTEGER, label TEXT, timeWindowNum TEXT)";
            arg2.execSQL(this.a);
            v0_1 = true;
        } catch (SQLException v0) {
            by.e("create aggregated table error!");
            v0_1 = false;
        }

        return v0_1;
    }

    public void onCreate(SQLiteDatabase arg2) {
        try {
            arg2.beginTransaction();
            this.c(arg2);
            this.b(arg2);
            this.a(arg2);
            arg2.setTransactionSuccessful();
        } catch (Exception v0_1) {
            v0_1.printStackTrace();
        } finally {
            arg2.endTransaction();
        }
    }

    public void onUpgrade(SQLiteDatabase arg1, int arg2, int arg3) {
    }
}

